using AntDesign;
using AntDesign.Select.Internal;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using SE.PO.CInterface.DBProtocols.Signals;
using SE.PO.CInterface.Enums;
using SE.PO.CInterface.Web.Pages.DB.Components;
using SE.PO.CInterface.Web.Pages.Form;

namespace SE.PO.CInterface.Web.Pages.DB.Signal
{
    public partial class SyncSignalFrom
    {
        private SelectSignalCoding _selectSignalCodingComponent= null!;
        
        private SelectVariables _selectVariablesComponent = null!;

        private SelectDevices _selectDevicesComponent = null!;

        private List<SelectMode> _siteType = null!;

        private List<SelectMode> _nodeFeatures = null!;

        [Parameter] public EventCallback OnSuccess { get; set; }

        [Parameter] public EventCallback OnFailure { get; set; }

        [Parameter] public EventCallback OnCancel { get; set; }

        public Form<CreateUpdateSignalDto> Form;

        public CreateUpdateSignalDto Model = new();

        private readonly FormItemLayout _formItemLayout = new FormItemLayout
        {
            LabelCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24},
                Sm = new EmbeddedProperty {Span = 7},
            },

            WrapperCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24},
                Sm = new EmbeddedProperty {Span = 12},
                Md = new EmbeddedProperty {Span = 10},
            }
        };

        private readonly FormItemLayout _submitFormLayout = new FormItemLayout
        {
            WrapperCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24, Offset = 0},
                Sm = new EmbeddedProperty {Span = 10, Offset = 7},
            }
        };

        

        protected override async Task OnInitializedAsync()
        {
        }

        public async Task<bool> SyncSignals()
        {
            var syncSignals = from s in _selectVariablesComponent.SelectedVariablesDto
                let deviceDto= _selectDevicesComponent.SelectedDevices.FirstOrDefault(d=>d.Equipment.Equals(s.Equipment))
                select new CreateUpdateSignalDto
                {
                    DeviceId = deviceDto.Id,
                    SiteId = Model.SiteId,
                    ScId = Model.ScId,
                    Type = Model.Type,
                    Id = $"{Model.ScId}{deviceDto.SuId}{deviceDto.Id}{_selectSignalCodingComponent.SelectedMonitorInfoCode}{s.Order}",
                    SignalName =s.Tag,
                    Describe = s.Comment,
                    Property = _selectSignalCodingComponent.SelectedAlarmProperity
                };
            var ok = await Client.SyncSignals(syncSignals);
            return ok;
        }

        private async Task OnFinish()
        {
            try
            {
                var variables = _selectVariablesComponent.SelectedVariablesDto;
                var signalDtos = from e in variables
                    select new CreateUpdateSignalDto()
                    {
                        ScId = Model.ScId,
                        // SignalTypeEnum = Model.SignalTypeEnum,
                        // Order = e.Order,
                        // SiteId = Model.SiteId,
                        // RoomId = Model.RoomId,
                        // SuId = Model.SuId,
                        // SignalName = e.Name,
                        // SignalDesc = $"{e.Cluster} {e.IOSignal}",
                    };
                var ok = await Client.SyncSignals(signalDtos);
                if (Form.Validate() && ok)
                {
                    await OnSuccess.InvokeAsync();
                }
                else
                {
                    await OnFailure.InvokeAsync();
                }
            }
            catch (System.Exception ex)
            {
                await Notice.Error(new()
                {
                    Duration = 100,
                    Message = "异常",
                    Description = ex.InnerException?.Message
                });
            }
        }

        private async Task OnFinishFailed(EditContext editContext)
        {
            await OnCancel.InvokeAsync();
        }

        private async Task GetDeviceSourceByTypeAsync(string selectDeviceType)
        {
            await _selectDevicesComponent.GetSourceAsync( selectDeviceType);
        }

        private async Task RefreshVariableOrderAsync(string monitorItem)
        {
            await _selectVariablesComponent.GetSourceAsync();
        }
       
    }
}